package cn.icesky1stm.isblog.utils;

import cn.icesky1stm.isblog.pojo.BlLog;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: ThreadLocalContext <br>
 * @Description: 获取线程内变量 <br>
 * @Package: cn.icesky1stm.isblog.utils <br>
 * @author: suitianmou <br>
 * @Date: 2021/6/29-10:01 <br>
 * @version: 1.0 <br>
 * @see <br>
 * @since (从产品的哪一个版本此类被添加进来 ( 可选))<br>
 * 修改历史: <br>
 * 修改时间             修改人           版本变更           修改说明<br>
 * -----------------------------------------------------<br>
 * <br>
 */
public class ThreadLocalContext {

    /**
     * 日志实体，此处写法很奇怪，util不应该参杂业务内容才对，先跟着教程这么写，后续再去其糟粕。
     */
    private BlLog logger = new BlLog();

    /**
     * 是否记录日志
     */
    private boolean isLog = false;

    /**
     * 线程本地内存中的变量
     */

    private static ThreadLocal<ThreadLocalContext> threadLocal = new ThreadLocal<>();

    public static ThreadLocalContext get(){
        if( threadLocal.get() == null){
            ThreadLocalContext threadLocalContext = new ThreadLocalContext();
            threadLocal.set(threadLocalContext);
        }
        ThreadLocalContext threadLocalContext = threadLocal.get();
        return threadLocalContext;
    }
    public void remove(){
        this.logger = null;
        this.isLog = false;
        threadLocal.remove();
    }

    public BlLog getLogger() {
        return logger;
    }

    public void setLogger(BlLog logger) {
        this.logger = logger;
    }

    public boolean isLog() {
        return isLog;
    }

    public void setLog(boolean log) {
        isLog = log;
    }
}


